<template>
  <a-modal
    title='编辑'
    :visible='visible'
    ok-text='确定'
    width="700px"
    @ok='handleSubmit'
    @cancel='handleCancel'
  >
    <a-form-model ref='form' :model='model' :rules='validatorRules'>
      <a-form-model-item label='项目类型' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='typeName'>
        <a-input placeholder='请输入项目类型' v-model='model.typeName'></a-input>
      </a-form-model-item>
      <a-form-model-item label='项目内容' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='checkContent'>
        <a-textarea placeholder='请输入项目内容' v-model='model.checkContent'></a-textarea>
      </a-form-model-item>
      <a-form-model-item label='项目分值' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='score'>
        <a-input-number placeholder='请输入项目分值' v-model="model.score" :max="100" style="width: 100%" :mix="0"/>
      </a-form-model-item>
      <a-form-model-item label='计分标准' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='planNo'>
        <a-table
          ref='table'
          size='middle'
          bordered
          rowKey='id'
          :columns='columns'
          :dataSource='dataSource'
          :pagination='ipagination'
          :loading='loading'
          :scroll="{ x: 450,y:300 }"
        >
          <span slot='actionName'>
            <a-button @click='openScoreModel' style='padding: 0 5px' size='small' type='primary'
                      icon='plus'>新增</a-button>
          </span>
          <span slot='action' slot-scope='text, record'>
          <a @click="handleEdit(record,'人员编辑')">编辑</a>
          <a-divider type='vertical'/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          </span>
        </a-table>
      </a-form-model-item>
    </a-form-model>
    <scoreModel ref='scoreModel' @ok="modalFormOk"></scoreModel>
  </a-modal>
</template>

<script>
  import {httpAction, getAction, postAction, deleteAction} from '@/api/manage'
  import columns from './editProjectColumns'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import scoreModel from './scoreModel'


  export default {
    name: 'editProject',
    mixins: [JeecgListMixin],
    components: {scoreModel},
    data() {
      return {
        model: {},
        columns: columns(this),
        visible: false,
        validatorRules: {
          typeName: [{required: true, message: '请输入!'}],
          checkContent: [{required: true, message: '请输入!'}],
          score: [{required: true, message: '请输入!'}]
        },
        labelCol: {span: 5},
        wrapperCol: {span: 17},
        url: {
          list: '/performcheck/checkScoreNorm/queryNormList',
          add: '/performcheck/checkTypeInfo/createType',
          edit: '/performcheck/checkTypeInfo/updateType',
          delete: '/performcheck/checkScoreNorm/deleteNorm'
        },
        dataSource: [],
      }
    },
    created() {
    },
    methods: {
      handleEdit(record) {
        this.$refs.scoreModel.edit(record)
      },
      openScoreModel() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let url, type
            if (!this.model.id) {
              url = this.url.add
              type = 'post'
            } else {
              url = this.url.edit
              type = 'put'
            }
            httpAction(url, this.model, type).then((res) => {
              if (res.success) {
                this.model.id = res.result
                this.$refs.scoreModel.add(res.result)
              } else {
                this.$message.error(res.message);
              }
            })
          }
        })
      },
      add(queryParam) {
        this.dataSource = []
        this.model = {}
        this.model.projectId = queryParam.projectId
        this.visible = true
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.loadData()
        this.visible = true
      },
      loadData() {
        this.loading = true;
        getAction(this.url.list, {typeId: this.model.id}).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result;
            if (res.result.total) {
              this.ipagination.total = res.result.total;
            } else {
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }
          this.loading = false;
        })
      },
      handleSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let url, type
            if (!this.model.id) {
              url = this.url.add
              type = 'post'
            } else {
              url = this.url.edit
              type = 'put'
            }
            httpAction(url, this.model, type).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.message);
              }
            })
          }
        })
      },
      handleCancel(e) {
        this.visible = false
      }
    }
  }
</script>

<style scoped>

</style>
